/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.states.menu;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Node;
import com.jme3.math.Vector3f;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.post.filters.DepthOfFieldFilter;
import com.jme3.renderer.queue.RenderQueue;
import com.jme3.texture.Texture;
import com.jme3.water.WaterFilter;
import etherblocks.engine.*;
import etherblocks.engine.gui.*;
import etherblocks.engine.noises.*;
import etherblocks.engine.objects.*;
import etherblocks.engine.states.*;
import etherblocks.game.blocks.*;
import com.cubes.*;

/**
 *
 * @author Carl
 */
public class StartMenuAppState extends BaseAppState{

    public StartMenuAppState(){
        
    }
    private Node terrainNode;
    
    @Override
    public void initialize(AppStateManager stateManager, Application application){
        super.initialize(stateManager, application);
        mainApplication.setDisplayFps(false);
        mainApplication.setDisplayStatView(false);
        mainApplication.getCamera().setLocation(new Vector3f(22.54f, 44.43f, 22.32f));
        mainApplication.getCamera().lookAtDirection(new Vector3f(0.768f, -0.03f, 0.637f), Vector3f.UNIT_Y);
        mainApplication.getFlyByCamera().setEnabled(false);
        NiftyAppState niftyAppState = getAppState(NiftyAppState.class);
        niftyAppState.createNifty("Interface/Menu.xml");
        //Light
        LightAppState lightAppState = new LightAppState();
        lightAppState.setAmbientLightColor(ColorRGBA.White.mult(3));
        lightAppState.setDirectionalLightColor(ColorRGBA.White.mult(0.2f));
        lightAppState.setLightDirection(new Vector3f(-0.6f, -1, 0.7f).normalizeLocal());
        lightAppState.setShadowsIntensity(0.3f);
        mainApplication.getStateManager().attach(lightAppState);
        //PostFilter
        PostFilterAppState postFilterAppState = new PostFilterAppState();
        WaterFilter waterFilter = new WaterFilter(mainApplication.getRootNode(), new Vector3f(-1, -5, -1).normalizeLocal());
        waterFilter.setWaterHeight(32.5f);
        //postFilterAppState.addFilter(waterFilter);
        DepthOfFieldFilter depthOfFieldFilter = new DepthOfFieldFilter();
        depthOfFieldFilter.setFocusDistance(0);
        depthOfFieldFilter.setFocusRange(20);
        //postFilterAppState.addFilter(depthOfFieldFilter);
        mainApplication.getStateManager().attach(postFilterAppState);
        //Sky
        SkyAppState skyAppState = new SkyAppState();
        skyAppState.setAnimationDuration(10);
        skyAppState.setColors(ColorRGBA.Blue, ColorRGBA.Cyan, ColorRGBA.Pink, ColorRGBA.Red);
        mainApplication.getStateManager().attach(skyAppState);
        //Clouds
        mainApplication.getStateManager().attach(new CloudsAppState());
        //BlockTerrain
        Material blockMaterial = new BlockMaterial(mainApplication.getAssetManager());
        Texture texture = mainApplication.getAssetManager().loadTexture("Textures/terrain1.png");
        texture.setMagFilter(Texture.MagFilter.Nearest);
        texture.setMinFilter(Texture.MinFilter.NearestNoMipMaps);
        blockMaterial.setTexture("DiffuseMap", texture);
        CubesSettings cubesSettings = CubesConfiguration.getSettings(mainApplication);
        cubesSettings.setBlockMaterial(blockMaterial);
        BlockTerrainControl blockTerrain = new BlockTerrainControl(cubesSettings, new Vector3Int(20, 1, 20));
        Vector3Int tmpVectorLocation = new Vector3Int();
        Vector3Int tmpVectorSize = new Vector3Int();
        PerlinNoise perlin = new PerlinNoise(179);
        for(int x=0;x<200;x++){
            for(int z=0;z<200;z++){
                tmpVectorLocation.set(x, 0, z);
                double value = (perlin.noise((x / 100d), (z / 96d)) + 0.5);
                tmpVectorSize.set(1, (int) ((value * 40) + 5), 1);
                blockTerrain.setBlockArea(tmpVectorLocation, tmpVectorSize, Block_Grass.class);
            }
        }
        terrainNode = new Node();
        terrainNode.addControl(blockTerrain);
        terrainNode.setShadowMode(RenderQueue.ShadowMode.CastAndReceive);
        mainApplication.getRootNode().attachChild(terrainNode);
    }

    @Override
    public void cleanup(){
        super.cleanup();
        mainApplication.setDisplayFps(true);
        mainApplication.setDisplayStatView(true);
        NiftyAppState niftyAppState = getAppState(NiftyAppState.class);
        ScreenController_StartGame screenController_StartGame = niftyAppState.getScreenController(ScreenController_StartGame.class);
        if(screenController_StartGame != null){
            screenController_StartGame.goToScreen("ingame");
            mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(LightAppState.class));
            mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(PostFilterAppState.class));
            mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(SkyAppState.class));
            mainApplication.getStateManager().detach(mainApplication.getStateManager().getState(CloudsAppState.class));
            mainApplication.getRootNode().detachChild(terrainNode);
        }
    }
}
